package evaluation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class EvaluationTool {
	public static Map<String, Double> evaluate(List<Integer> labelList, List<Integer> predictList) {
		Iterator<Integer> lItr = labelList.iterator();
		Iterator<Integer> pItr = predictList.iterator();
		
		int tp = 0;
		int tn = 0;
		int fp = 0;
		int fn = 0;
		while(pItr.hasNext()) {
			int l = lItr.next();
			int p = pItr.next();
			
			if(l == 1) {
				if(p == 1) {
					tp ++;
				} else {
					fn ++;
				}
			} else {
				if(p == 1) {
					fp ++;
				} else {
					tn ++;
				}
			}
		}
		
		Map<String, Double> result = new HashMap<String, Double>();
		result.put("tp", Double.valueOf(tp));
		result.put("tn", Double.valueOf(tn));
		result.put("fp", Double.valueOf(fp));
		result.put("fn", Double.valueOf(fn));
		
		double accuracy = (tp + tn) / (double)(tp + tn + fp + fn);
		double recall = tp / (double)(tp + fn);
		double precision = tp / (double)(tp + fp);
		double fscore = 2 * recall * precision / (recall + precision);
		
		double pmiss = 1 - recall;
		double pfp = 1 - precision;
		double dcr = pmiss + 0.005 * pfp;
		
		double frecall = tn / (double) (tn + fp);
		
		double info = frecall / pmiss;
		
		result.put("accuracy", accuracy);
		result.put("recall", recall);
		result.put("precision", precision);
		result.put("fscore", fscore);
		result.put("pmiss", pmiss);
		result.put("pfp", pfp);
		result.put("dcr", dcr);
		result.put("frecall", frecall);
		result.put("info", info);
		
		return result;
	}
}
